package org.gjy.m8.thread;

import org.junit.Test;

import java.util.concurrent.CountDownLatch;

/**
 * await:
 * 当所有线程都调用了CountDownLatch对象的countDown方法后，也就是计数器的值为0时；
 * 其他线程调用了当前线程的interrupt（）方法中断了当前线程，当前线程就会抛出InterruptedException异常，然后返回。
 * 设置的timeout时间到了，因为超时而返回false
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-31 20:51:01
 */
public class CountDownLatchTest {

    @Test
    public void test1() throws InterruptedException {
        Thread main = Thread.currentThread();

        CountDownLatch cdl = new CountDownLatch(3);
        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName());
                cdl.countDown();
                main.interrupt();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, "A3").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName());
            cdl.countDown();
        }, "A1").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName());
            cdl.countDown();
        }, "A2").start();

        System.out.println("main start");
        cdl.await();
        System.out.println("main end");
    }
}
